/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.diego.game.sudoku.dao;

import br.diego.game.sudoku.Sudoku;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author diego
 */
public class DAO {

    private RecordStore recordStore;
    private static DAO DAO;

    public static DAO getInstance() {
        if (DAO == null) {
            DAO = new DAO();
        }
        return DAO;
    }

    private DAO() {
        this("SUDOKU");
    }

    public void udpate(int idRecord, Sudoku sudoku) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            byte[] record;

            int tab[][] = sudoku.getTabuleiro();
            for (int i = 0; i < tab.length; i++) {
                int[] is = tab[i];
                for (int j = 0; j < is.length; j++) {
                    int k = is[j];
                    dos.writeInt(k);
                }
            }
            boolean edits[][] = sudoku.getEdits();
            for (int i = 0; i < edits.length; i++) {
                boolean[] is = edits[i];
                for (int j = 0; j < is.length; j++) {
                    boolean k = is[j];
                    dos.writeBoolean(k);
                }
            }

            dos.writeInt(sudoku.getX());
            dos.writeInt(sudoku.getY());

            record = baos.toByteArray();
            recordStore.setRecord(idRecord, record, 0, record.length);
            baos.reset();
            baos.close();
            dos.close();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }

    public Sudoku get(int idRecord) throws IOException, InvalidRecordIDException, RecordStoreException {
        int[][] tab = new int[9][9];
        boolean[][] bols = new boolean[9][9];

        int size = recordStore.getRecordSize(idRecord);
        byte[] registro = new byte[size];

        ByteArrayInputStream bais = new ByteArrayInputStream(registro);
        DataInputStream dis = new DataInputStream(bais);
        recordStore.getRecord(idRecord, registro, 0);

        for (int i = 0; i < tab.length; i++) {
            for (int j = 0; j < tab.length; j++) {
                tab[i][j] = dis.readInt();

            }
        }
        for (int i = 0; i < bols.length; i++) {
            for (int j = 0; j < bols.length; j++) {
                bols[i][j] = dis.readBoolean();
            }
        }
        int x = dis.readInt();
        int y = dis.readInt();

        bais.reset();
        bais.close();
        dis.close();
        final Sudoku sudoku = new Sudoku(tab, bols);
        sudoku.setXY(x, y);
        return sudoku;
    }

    public int getSize() {
        try {
            final int numRecords = recordStore.getNumRecords();
            return numRecords;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public void add(Sudoku sudoku) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            byte[] record;
            int tab[][] = sudoku.getTabuleiro();
            for (int i = 0; i < tab.length; i++) {
                int[] is = tab[i];
                for (int j = 0; j < is.length; j++) {
                    int k = is[j];
                    dos.writeInt(k);
                }
            }
            boolean edits[][] = sudoku.getEdits();
            for (int i = 0; i < edits.length; i++) {
                boolean[] is = edits[i];
                for (int j = 0; j < is.length; j++) {
                    boolean k = is[j];
                    dos.writeBoolean(k);
                }
            }
            dos.writeInt(sudoku.getX());
            dos.writeInt(sudoku.getY());
            record = baos.toByteArray();
            recordStore.addRecord(record, 0, record.length);
            baos.reset();
            baos.close();
            dos.close();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public DAO(String bd) {
        try {
            recordStore = RecordStore.openRecordStore(bd, true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public void close() throws RecordStoreNotOpenException, RecordStoreException {
        recordStore.closeRecordStore();
        DAO = null;
    }
}
